const { defineConfig } = require('@vue/cli-service')
const fs=require("fs")
const user=require('./user.json')
const MockData = require("./src/mock/index")
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    onBeforeSetupMiddleware: function (devServer) {
      if (!devServer) {
        throw new Error('webpack-dev-server is not defined');
      }
      devServer.app.get("/api/list",(req,res)=>{
        res.send(MockData.list)
        console.log(MockData.list)
      })
      devServer.app.get('/api/login', function (req, res) {
        let { username, password } = req.query
        console.log(username)
        let flag = user.some((item) => item.username === username && item.password === password)
        if (flag) {
          res.send({
            code: 200,
            message: "登录成功",
            token:{
              username,password
            }
          })
        }else{
          res.send({
            code: 1,
            message: "您还没有注册，请您去注册"
          })
        }
      });
      devServer.app.get('/api/zhuce', function (req, res) {
        let { username, password } = req.query
        let flag = user.some((item) => item.username === username)
        if (flag) {
          res.send({
            code: 201,
            message: "已经注册过了 户直接去登陆吧"
          })
        }
        else{
          user.push({ username, password } )
          fs.writeFileSync("./user.json",JSON.stringify(user))
          res.send({
            code: 202,
            message: "注册成功"
          })
        }
       
      });
      // devServer.app.get('/api/getitem', function (req, res) {
      //   let arr=MockData.list.children.filter((item) => {
      //     return item.id==req.query.id
      //   })
      //   res.json(arr[0]);
      // });
    },
  },
})
